//
//  HuiKPlanV.m
//  BrushFace
//
//  Created by timer_q on 16/1/17.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "HuiKPlanV.h"
#import "HuiKuanHeaderV.h"
#import "HuiKuanFooterV.h"
#import "HuiKuanCell.h"

@implementation HuiKPlanV
{
    UITableView *_tableView;
    CGFloat viewHeight;
    HuiKuanHeaderV *huiKuanHeader;
    HuiKuanFooterV *huiKuanFoot;
    NSMutableArray *_dataArray;
    UITableView *_baseTable;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        viewHeight = frame.size.height;
        _dataArray = [NSMutableArray array];
        [self UIConfig];
    }
    return self;
}
- (void)UIConfig{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, viewHeight) style:UITableViewStylePlain];
    huiKuanHeader = [HuiKuanHeaderV loadBundleNib];
    huiKuanHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, 260);
    huiKuanFoot = [HuiKuanFooterV loadBundleNib];
    huiKuanFoot.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
    _tableView.tableFooterView = huiKuanFoot;
    _tableView.tableHeaderView = huiKuanHeader;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_tableView];
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HuiKuanCell *cell = [HuiKuanCell loadBundleNib];
    NSDictionary *subDict = [_dataArray objectAtIndex:indexPath.row];
    cell.moneyLabel.text = [NSString stringWithFormat:@"%@",[subDict objectForKey:@"capital"]];
    cell.dateLabel.text = [NSString stringWithFormat:@"%@",[subDict objectForKey:@"plandate"]];
    cell.lixiLabel.text = [NSString stringWithFormat:@"%@",[subDict objectForKey:@"interest"]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (void)setHuiKuanData:(id)data{
    huiKuanHeader.fangshiLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还款方式：%@",[data objectForKey:@"incomeType"]] andlocation:5 andFirstColor:[UIColor blackColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.qiciLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还款期次：%@次",[data objectForKey:@"incomecount"]] andlocation:5 andFirstColor:[UIColor blackColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.startLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还款开始日期：%@",[self timerStrWithTime:[data objectForKey:@"startincomedate"]]] andlocation:7 andFirstColor:[UIColor blackColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.stopLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还款截止日期：%@",[self timerStrWithTime:[data objectForKey:@"endincomedate"]]] andlocation:7 andFirstColor:[UIColor blackColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.yijinghuikuanLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"已经回款：%@",[data objectForKey:@"realincomeamount"]] andlocation:5 andFirstColor:[UIColor redColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.haixuhuikuanLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还需回款：%@",[data objectForKey:@"dutoincomeamount"]] andlocation:5 andFirstColor:[UIColor redColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.xiaqihuikuanLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"下期回款：%@",[data objectForKey:@"nextincomeamount"]] andlocation:5 andFirstColor:[UIColor redColor] andLastColor:[UIColor lightGrayColor]];
    
    huiKuanFoot.yingshouLabel.text = [NSString stringWithFormat:@"应还款总额:%@(含本金¥%@、收益¥%@)",[data objectForKey:@"totalIncomeamountView"],[data objectForKey:@"totalCapitalView"],[data objectForKey:@"totalInterestView"]];
    huiKuanFoot.jiangliLabel.text = [NSString stringWithFormat:@"奖励：本金¥0、收益¥0"];
    
    [_dataArray removeAllObjects];
    [_dataArray addObjectsFromArray:[data objectForKey:@"data"]];
    [_tableView reloadData];


}
- (NSMutableAttributedString *)labelStr:(NSString *)baseStr andlocation:(NSInteger)location andFirstColor:(UIColor *)fc andLastColor:(UIColor *)lc{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:baseStr];
    [str addAttribute:NSForegroundColorAttributeName value:fc range:NSMakeRange(0, location)];
    [str addAttribute:NSForegroundColorAttributeName value:lc range:NSMakeRange(location, str.length-location)];
    return str;
    
}


- (NSString *)timerStrWithTime:(NSString *)time{
    if (!time.isNotEmpty) {
        return @"后台返回了空值";
    }
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:[time doubleValue]/1000+28800];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}
- (void)baseTableView:(UITableView *)baseTable{
    _baseTable = baseTable;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_tableView.contentOffset.y<0) {
        _baseTable.contentOffset = _tableView.contentOffset;
    }
}

@end
